﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using WatiNCF.Automater.Controls;

namespace WatiNCF.Automater
{
    public partial class ParameterInputForm : Form
    {
        public object Target { get; set; }
        public MethodInfo Method { get; set; }

        public List<object> GetParameterValues()
        {
            List<object> values = new List<object>();
            foreach (var control in ParametersPanel.Controls.OfType<BaseDataEntryControl>().Reverse())
            {
                values.Add(control.GetValue());
            }
            return values;
        }

        private void ShowParameters(ParameterInfo[] parameters)
        {
            this.ParametersPanel.Controls.Clear();
            int count = parameters.Length;
            foreach (var paramInfo in parameters.Reverse())
            {
                BaseDataEntryControl control = DataEntryControlFactory.GetControl(paramInfo);
                if (control != null)
                {
                    control.Dock = DockStyle.Top;
                    control.TabIndex = count;
                    this.ParametersPanel.Controls.Add(control);
                    IncreaseFormSize(control.Height);

                    if (count == 1)
                    {
                        control.SetFocus();
                    }
                }
                count--;
            }
        }

        private void IncreaseFormSize(int height)
        {
            this.Height += height;
        }

        public ParameterInputForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RenameForm();
            ShowParameters(Method.GetParameters());
            RepositionForm();
        }

        private void RenameForm()
        {
            this.Text = string.Format(this.Text, this.Method.Name);
        }

        private void RepositionForm()
        {
            Rectangle rect = Screen.GetWorkingArea(this);
            this.Location = new Point(rect.Right - this.Size.Width, rect.Bottom - this.Size.Height);
        }
    }
}
